package adapters;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.view.ViewGroup;

import java.util.List;

import objects.CostItem;

public class CostPagerAdapter extends FragmentPagerAdapter {

    private List<CostItem> costItemList;
    private Context mContext;

    public CostPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.mContext = context;
    }

    public void setCostItems(List<CostItem> costItems) {
        this.costItemList = costItems;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        switch (position) {
            case 1:
                CostPlaceholderFragment costPlaceholderFragment = (CostPlaceholderFragment) super.instantiateItem(container, position);
                costPlaceholderFragment.onAttach(mContext);
                if (costItemList != null && costItemList.size() > 0) {
                    costPlaceholderFragment.setList(costItemList);
                }
                break;
            default:
                break;
        }
        return super.instantiateItem(container, position);
    }

    @Override
    public Fragment getItem(int position) {
        return new CostPlaceholderFragment().newInstance(position + 1);
    }


    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "项目";
            case 1:
                return "明细";
            case 2:
                return "一日账";
        }
        return null;
    }

    @Override
    public int getItemPosition(Object object) {
        return PagerAdapter.POSITION_NONE;
    }
}
